package com.aistar.serviceVO.impl;

import com.aistar.mapper.CustomerMapper;
import com.aistar.mapper.DynamicMapper;
import com.aistar.mapper.DynamicPraiseMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.Dynamic;
import com.aistar.pojo.DynamicPraise;
import com.aistar.pojo.DynamicPraiseExample;
import com.aistar.pojoVO.DynamicAndPraiseVO;
import com.aistar.serviceVO.DynamicAndPraiseVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicAndPraiseVOServiceImpl implements DynamicAndPraiseVOService {
    private final DynamicPraiseMapper dynamicPraiseMapper;
    private final DynamicMapper dynamicMapper;
    private final CustomerMapper customerMapper;

    @Autowired
    public DynamicAndPraiseVOServiceImpl(
            DynamicPraiseMapper dynamicPraiseMapper,
            DynamicMapper dynamicMapper,
            CustomerMapper customerMapper) {
        this.dynamicPraiseMapper = dynamicPraiseMapper;
        this.dynamicMapper = dynamicMapper;
        this.customerMapper = customerMapper;
    }


    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        DynamicPraiseExample example = new DynamicPraiseExample();
        example.createCriteria()
                .andCustomerIdEqualTo(customerId)
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS);
        example.setOrderByClause("praise_time desc");
        List<DynamicPraise> dynamicPraiseList = dynamicPraiseMapper.selectByExample(example);
        if (dynamicPraiseList != null && dynamicPraiseList.size() > 0) {
            List<DynamicAndPraiseVO> dynamicAndPraiseVOList = new ArrayList<>();
            for (DynamicPraise dynamicPraise : dynamicPraiseList) {
                Dynamic dynamic = dynamicMapper.selectByPrimaryKey(dynamicPraise.getDynamicId());
                Customer customer = customerMapper.selectByPrimaryKey(dynamicPraise.getCustomerId());
                dynamicAndPraiseVOList.add(new DynamicAndPraiseVO(dynamicPraise, dynamic, customer));
            }
            return ServerResponse.getSuccess(dynamicAndPraiseVOList);
        }
        return ServerResponse.getFailed();
    }
}
